package cn.wzvtc.ch10findby.controller;

import cn.wzvtc.ch10findby.entity.User;
import cn.wzvtc.ch10findby.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserRepository userRepository;

    //根据编号进行查询
    @GetMapping("/findByCode")
    public User findByCode(String code) {
        return userRepository.findByCode(code);
    }

    //根据用户名模糊查询
    @GetMapping("/findByUserNameLike")
    public List<User> findByUserNameLike(String username) {
        return userRepository.findByUserNameLike("%" + username + "%");
    }

    //根据性别查询
    @GetMapping("/findByGender")
    public List<User> findByUserNameAndGender(Integer gender) {
        return userRepository.findByGender(gender);
    }

    //根据用户名和性别和年龄段
    @GetMapping("/findByUserNameAndGenderAndAgeBetween")
    public List<User> findByUserNameAndGenderAndAgeBetween(String userName, Integer gender,
                                                           Integer minAge, Integer maxAge) {
        return userRepository.findByUserNameAndGenderAndAgeBetween(userName, gender, minAge, maxAge);
    }

}
